package com.yanqu.road.server.manger.activity.langyabang;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.langyabang.config.*;
import com.yanqu.road.entity.activity.langyabang.data.LangYaBangWarReportData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.logic.bussiness.activity.LangYaBangBusiness;

import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.LangYaBangProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.langyabang.LangYaBangModule;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.rank.YRankMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaBangMgr extends TempMgr {

    private static LangYaBangConfig config;

    private static ActivityInfo activityInfo;

    @Override
    public boolean init() throws Exception {
        return reload();
    }
    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }
    public static void reloadActivityData() {
        getLogger().info("reload LangYaBang activity start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.LangYaBang.getValue());
        if (list.isEmpty()) {
            config = null;
            activityInfo = null;
            getLogger().info("reload LangYaBang end, no activity in show time.");
            return;
        }
        ActivityInfo tmpActivityInfo = list.get(0);
        activityInfo = tmpActivityInfo;
        //活动配置
        int activityId = activityInfo.getActivityId();

        Map<Integer, LangYaCaseConfig> tmpCaseConfig = LangYaBangBusiness.getLangYaCaseConfigMap(activityId);
        Map<Integer,Map<Integer, LangYaCaseEventConfig>> tmpCaseEventConfig = LangYaBangBusiness.getLangYaCaseEventConfigMap(activityId);
        //区域配置
        Map<Integer,Map<Integer, LangYaBangAreaConfig>> areaConfigMap = LangYaBangBusiness.getLangYaBangAreaConfigMap(activityId);
        //总等级配置
        Map<Integer, LangYaBangBuildingConfig> buildingLevelConfigMap = LangYaBangBusiness.getLangYaBangBuildingConfigMap(activityId);
        //pve
        Map<Integer, LangYaBangPveConfig> pveConfigMap = LangYaBangBusiness.getLangYaBangPveConfigList(activityId);
        //求策
        Map<Integer,Map<Integer,LangYaRollDiceConfig>> rollDiceConfigMap= LangYaBangBusiness.getLangYaRollDiceConfigMap(activityId);
        //其他
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());

        config = new LangYaBangConfig(activityInfo,tmpCaseConfig,tmpCaseEventConfig,activityConfigMap,areaConfigMap,buildingLevelConfigMap,pveConfigMap,rollDiceConfigMap);



        getLogger().info("reload LangYaBang end, activityId {}.", activityInfo.getActivityId());

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(LangYaBangModule.class).reloadActivity();
        }
    }

    public static LangYaBangProto.LangYaBangConfigSyncMsg.Builder getConfigMsg(String language){
        LangYaBangProto.LangYaBangConfigSyncMsg.Builder builder = LangYaBangProto.LangYaBangConfigSyncMsg.newBuilder();

        for (LangYaCaseConfig caseConfig:config.getCaseConfigMap().values()){
            LangYaBangProto.LangYaBangCaseConfig.Builder caseConfigBuilder = LangYaBangProto.LangYaBangCaseConfig.newBuilder();
            caseConfigBuilder.setCaseId(caseConfig.getId());
            caseConfigBuilder.addAllInitValue(caseConfig.getInitNum());
            caseConfigBuilder.addAllLoseNum1(caseConfig.getLoseNum1());
            caseConfigBuilder.addAllLoseNum2(caseConfig.getLoseNum2());
            caseConfigBuilder.setReward(caseConfig.getReward());
            caseConfigBuilder.setStar(caseConfig.getStar());
            builder.addCaseConfig(caseConfigBuilder);
        }
        for (Map<Integer, LangYaCaseEventConfig> caseEventConfigMap : config.getCaseEventConfig().values()){
            for (LangYaCaseEventConfig caseEventConfig:caseEventConfigMap.values()){
                LangYaBangProto.LangYaBangCaseEventConfig.Builder eventBuilder = LangYaBangProto.LangYaBangCaseEventConfig.newBuilder();
                eventBuilder.setEventId(caseEventConfig.getId());
                eventBuilder.setCaseId(caseEventConfig.getCaseId());
                eventBuilder.setQuestion(ServerLanguageMgr.getContent(caseEventConfig.getQuestion(),language));
                eventBuilder.setChooseA(ServerLanguageMgr.getContent(caseEventConfig.getChooseA(),language));
                eventBuilder.setJumpA(caseEventConfig.getJumpA());
                eventBuilder.addAllEffectA(caseEventConfig.getEffectA());
                eventBuilder.setChooseB(ServerLanguageMgr.getContent(caseEventConfig.getChooseB(),language));
                eventBuilder.setJumpB(caseEventConfig.getJumpB());
                eventBuilder.addAllEffectB(caseEventConfig.getEffectB());
                builder.addCaseEventConfig(eventBuilder);
            }
        }
        for (Map<Integer, LangYaBangAreaConfig> map :config.getAreaConfigMap().values()){
            for (LangYaBangAreaConfig langYaBangAreaConfig : map.values()){
                LangYaBangProto.LangYaBangAreaConfig.Builder areaBuilder = LangYaBangProto.LangYaBangAreaConfig.newBuilder();
                areaBuilder.setAreaLvId(langYaBangAreaConfig.getAreaLvId());
                areaBuilder.setCardLv(langYaBangAreaConfig.getCardLv());
                areaBuilder.setAreaType(langYaBangAreaConfig.getAreaType());
                areaBuilder.setCost(langYaBangAreaConfig.getCost());
                areaBuilder.setLv(langYaBangAreaConfig.getLv());
                areaBuilder.setUnlockPart(langYaBangAreaConfig.getUnlockPart());
                areaBuilder.setAreaValue(langYaBangAreaConfig.getAreaValue());
                areaBuilder.setUpLimit(langYaBangAreaConfig.getUpLimit());
                areaBuilder.setStep(langYaBangAreaConfig.getStep());
                areaBuilder.setReward(langYaBangAreaConfig.getReward());
                builder.addAreaConfig(areaBuilder);
            }
        }

        for (LangYaBangBuildingConfig buildingConfig:config.getBuildingLevelConfigMap().values()){
            LangYaBangProto.LangYaBangBuildingConfig.Builder buildingBuilder =  LangYaBangProto.LangYaBangBuildingConfig.newBuilder();
            buildingBuilder.setStationLv(buildingConfig.getStationLv());
            buildingBuilder.setEffect(buildingConfig.getEffect());
            buildingBuilder.setAddPowerPro(buildingConfig.getAddPowerPro());
            buildingBuilder.setAddPowerFix(buildingConfig.getAddPowerFix());
            buildingBuilder.setEnergyCostChoose(buildingConfig.getEnergyCostChoose());
            builder.addBuildingConfig(buildingBuilder);
        }

        for (LangYaBangPveConfig langYaBangPveConfig:config.getPveConfigMap().values()){
            LangYaBangProto.LangYaBangPveConfig.Builder pveConfigBuilder = LangYaBangProto.LangYaBangPveConfig.newBuilder();
            pveConfigBuilder.setNpcId(langYaBangPveConfig.getNpcId());
            pveConfigBuilder.setName(ServerLanguageMgr.getContent(langYaBangPveConfig.getName(),language));
            pveConfigBuilder.setDesc(ServerLanguageMgr.getContent(langYaBangPveConfig.getDesc(),language));
            pveConfigBuilder.setBaseCoin(langYaBangPveConfig.getBaseCoin());
            pveConfigBuilder.setExCoin(langYaBangPveConfig.getExCoin());
            pveConfigBuilder.setReward(langYaBangPveConfig.getReward());
            pveConfigBuilder.setQuality(langYaBangPveConfig.getQuality());
            builder.addPveConfig(pveConfigBuilder);
        }

        for (Map<Integer, LangYaRollDiceConfig> rollConfig:config.getRollDiceConfigMap().values()){
            for (LangYaRollDiceConfig diceConfig:rollConfig.values()){
                LangYaBangProto.LangYaRollDiceConfig.Builder rollBuilder = LangYaBangProto.LangYaRollDiceConfig.newBuilder();
                rollBuilder.setId(diceConfig.getId());
                rollBuilder.setType(diceConfig.getType());
                rollBuilder.setWeight(diceConfig.getWeight());
                rollBuilder.setEventParam(diceConfig.getEventParam());
                rollBuilder.setBaseProduce(diceConfig.getBaseProduce());
                rollBuilder.setReward(diceConfig.getReward());
                rollBuilder.setProtectNum(diceConfig.getProtectNum());
                rollBuilder.setUnlock(diceConfig.getUnlock());
                rollBuilder.setAddType(diceConfig.getAddType());
                rollBuilder.setQuality(diceConfig.getQuality());
                builder.addDiceConfig(rollBuilder);
            }
        }

        return builder;

    }

    public static Map<Integer,Integer> getInitCaseGameMap(){
        Map<Integer,Integer> initMap = new ConcurrentHashMap<>();
        if (config == null){
            return initMap;
        }
        Map<Integer, LangYaCaseConfig> caseConfigMap = config.getCaseConfigMap();
        if (caseConfigMap == null || caseConfigMap.isEmpty()){
            return initMap;
        }

        for (int configId:caseConfigMap.keySet()){
            initMap.put(configId,0);
        }
        return initMap;
    }

    public static LangYaCaseConfig getLangYaCaseConfig(int caseId){
        return config.getCaseConfigMap().get(caseId);
    }

    public static LangYaCaseEventConfig getLangYaCaseEventConfig(int caseId,int eventId){
        return config.getCaseEventConfig().get(caseId).get(eventId);
    }

    public static LangYaBangConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static void addWarReportToUser(LangYaBangWarReportData warReportData){
        LangYaBangBusiness.addLangYaBangWarReportData(warReportData);
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    public static void changeUserLangYaBangUnionScore(int activityId,long userId, long score){
        ActivityInfo activityInfo = LangYaBangMgr.getActivityInfo();
        if(null == activityInfo){
            return;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return;
        }
        if (activityInfo.getActivityId() != activityId){
            return;
        }
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if(null != player){
            player.notifyListener(eGamePlayerEventType.LangYaUnionScoreRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(score), player.getUserInfo().getUnionUid()));
        }else {
            List<UserActivityConditionData> userActivityConditionDataListByUserIdAndType = null;
            Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = NormalActivityMgr.getActivityConditionInfoMap(activityInfo.getActivityId());
            if(activityConditionInfoMap.size() > 0){
                List<ActivityConditionInfo> activityConditionInfoList = activityConditionInfoMap.get(eGamePlayerEventType.LangYaUnionScoreRank.getValue());
                userActivityConditionDataListByUserIdAndType = UserActivityBussiness.getUserActivityConditionDataListByUserIdAndType(activityInfo.getActivityId(), userId, eGamePlayerEventType.LangYaUnionScoreRank.getValue());
                if (userActivityConditionDataListByUserIdAndType == null || userActivityConditionDataListByUserIdAndType.isEmpty()){
                    UserActivityConditionData userConditionData = NormalActivityMgr.initUserActivityConditionData(userId, UserMgr.getNickName(userId), BigInteger.valueOf(score),
                            activityInfo.getActivityId(), activityConditionInfoList.get(0).getConditionId(), activityConditionInfoList.get(0).getType());
                    UserActivityBussiness.addUserActivityConditionData(userConditionData);
                } else {
                    for (UserActivityConditionData conditionData: userActivityConditionDataListByUserIdAndType){
                        conditionData.setValue(BigInteger.valueOf(score));
                        UserActivityBussiness.updateUserActivityConditionData(conditionData);
                    }
                }

            }
            if (null != userActivityConditionDataListByUserIdAndType){
                for (UserActivityConditionData conditionData : userActivityConditionDataListByUserIdAndType){
                    String unionUid = "";
                    UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(activityInfo.getActivityId(), userId);
                    if (unionData != null){
                        unionUid = unionData.getUnionUid();
                    }
                    YRankMgr.uploadUnionYRank(conditionData,unionUid,UnionRiseRankArgs.NOTIFY_TYPE_NORMAL);
                }
            }

        }


    }

    public static int getBuildingAddition(int activityId,Map<Integer,Integer>buildingLevelMap){
        if (config == null){
            return 0;
        }
        for (Map.Entry<Integer,Integer> entry: buildingLevelMap.entrySet()){
            LangYaBangAreaConfig langYaBangAreaConfig = config.getAreaConfigMap().get(entry.getKey()).get(entry.getValue());

        }
        return 0;
    }

    public static int getBuildingTotalLevel(List<LangYaBangProto.LangYaUserBuildingData> buildingData){
        if (config == null){
            return 0;
        }
        int totalLevel = 0;
        for (LangYaBangProto.LangYaUserBuildingData data: buildingData){
            LangYaBangAreaConfig langYaBangAreaConfig = config.getAreaConfigMap().get(data.getBuildingId()).get(data.getLevelId());
            totalLevel += langYaBangAreaConfig.getLv();
        }
        return totalLevel;
    }

    public static boolean canChangeUnion() {
        long now = System.currentTimeMillis();
        if (activityInfo != null) {
            if (now > activityInfo.getBeginTime() * 1000L && now < activityInfo.getEndTime() * 1000L) {
                return false;
            }

        }
        return true;
    }
    public static int getMaxNoticeAndReportNum (){
        int num = 50 ;
        if (config == null){
            return num;
        }
        num = config.getNoticNum();
        return num;
    }

    public static int getScoreItemId(){
        if (config == null){
            return eItemId.LANG_YA_BANG_SCORE_ITEM.getValue();
        }
        return config.getScoreItemId();

    }

    public static int getScorePerPower(){
        if (config == null){
            return 0;
        }
        return config.getScorePerPower();
    }

    public static int getDeclareWarBattleEnergyUsed(){
        if (config == null){
            return 0;
        }
        return config.getDeclareWarBattleEnergyUsed();
    }
}
